#!/bin/bash


if [[ ! -f /root/script/rabbitmqadmin ]];then
  wget http://10.202.94.141:30072/cli/rabbitmqadmin -O /root/script/rabbitmqadmin &&
  chmod 777 /root/script/rabbitmqadmin
fi
if [[ ! -f  /tmp/mon_rabbitmq ]];then
   touch  /tmp/mon_rabbitmq
fi
num=`cat /tmp/mon_rabbitmq`
let numzz=$num+1
echo $numzz > /tmp/mon_rabbitmq
./rabbitmqadmin -u guest -p guest -H 10.202.94.141 -P 30072 list queues name vhost messages --format=pretty_json > /tmp/data.json &&
cat  /tmp/data.json | jq -c '.[]' | while read -r person; do
    messages=$(echo "$person" | jq -r '.messages')
    name=$(echo "$person" | jq -r '.name')
    vhost=$(echo "$person" | jq -r '.vhost')
    if [[ $messages -gt 100 && $vhost != "/oms4"* ]];then
      echo "messages: $messages, vhost: $vhost, Name: $name"
      if [[ `cat /tmp/mon_rabbitmq` -gt 3 ]];then
        ssh -o "StrictHostKeyChecking no" root@10.202.16.154 "echo "${vhost}-${name} Blockage occurred, the current queue message is ${messages}"|mail -s RabbitMQ\ message\ is\  blocked\ in\ the\ development\ testing\ environment zhang.zhe@y3.sg,yi.he@y3.sg,guocheng.liang@y3.sg"
        echo "1" > /tmp/mon_rabbitmq
      fi
      #./rabbitmqadmin -u guest -p guest -H 10.202.94.141 -P 30072 --vhost=$vhost delete queue name=$name &&
      #./rabbitmqadmin -u guest -p guest -H 10.202.94.141 -P 30072 --vhost=$vhost declare queue name=$name durable=true
    fi
done
